Skip to content

Validate the elements in Date#marshal_load - #204

Open
mame wants to merge 3 commits into
ruby:masterfrom
mame:date-marshal-load-typecheck
Open

mame wants to merge 3 commits into
ruby:masterfrom
mame:date-marshal-load-typecheck

Conversation

@mame

@mame mame commented Nov 13, 2026

Copy link
Copy Markdown
Member

The six-element form stored nth and sf without checking their types, and accepted values that no Date or DateTime can have. Require an Integer for nth and an Integer or Rational for sf, and reject jd, df, sf, of and sg out of the ranges that a Date or DateTime can have.

Reported-by: Hiroshi SHIBATA hsbt@ruby-lang.org

@mame
mame requested a review from nobu November 13, 2026 02:54
The six-element form stored `nth` and `sf` without checking their
types, and accepted values that no Date or DateTime can have. Require
an Integer for `nth` and an Integer or Rational for `sf`, and reject
`jd`, `df`, `sf`, `of` and `sg` out of the ranges that a Date or
DateTime can have.

Reported-by: Hiroshi SHIBATA <hsbt@ruby-lang.org>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the date-marshal-load-typecheck branch from aca6f17 to b737125 Compare November 13, 2026 04:09
Comment thread ext/date/date_core.c
Comment on lines +7828 to +7842
if (!RB_INTEGER_TYPE_P(nth))
rb_raise(rb_eTypeError, "invalid nth (expected an Integer)");
if (jd < -1 || jd > CM_PERIOD)
rb_raise(eDateError, "invalid jd");
if (df < 0 || df >= DAY_IN_SECONDS)
rb_raise(eDateError, "invalid day fraction");
if (!RB_INTEGER_TYPE_P(sf) && !RB_TYPE_P(sf, T_RATIONAL))
rb_raise(rb_eTypeError, "invalid second fraction (expected an Integer or Rational)");
if (f_lt_p(sf, INT2FIX(0)) ||
f_ge_p(sf, INT2FIX(SECOND_IN_NANOSECONDS)))
rb_raise(eDateError, "invalid second fraction");
if (of < -DAY_IN_SECONDS || of > DAY_IN_SECONDS)
rb_raise(eDateError, "invalid offset");
if (!c_valid_start_p(sg))
rb_raise(eDateError, "invalid start");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation here looks weird. If it's consistent with the rest of the file, I guess it is fine, but otherwise I recommend indenting the if body further than the if clause.

@nobu nobu Nov 17, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GitHub shows tabs wrongly.

Comment thread ext/date/date_core.c Outdated

if (!RB_INTEGER_TYPE_P(nth))
rb_raise(rb_eTypeError, "invalid nth (expected an Integer)");
if (jd < -1 || jd > CM_PERIOD)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Date should support negative jd. Does Marshal.load(Marshal.dump(Date.jd(-2))) still work with this (maybe there is a different marshal encoding for this, I'm not sure)?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, Date.jd(-2) still round-trips:

d = Date.jd(-2)
d.marshal_dump[0, 2]               # => [-1, 213447715]
Marshal.load(Marshal.dump(d)) == d # => true

assert_equal(Date.new(10**20), Date.allocate.marshal_load(Date.new(10**20).marshal_dump))
[
DateTime.new(-4712, 1, 1, 0, 0, 0, "+12:00"),
DateTime.jd(213447716, 23, 59, 59, "-12:00"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the negative jd case:

Suggested change
DateTime.jd(213447716, 23, 59, 59, "-12:00"),
DateTime.jd(213447716, 23, 59, 59, "-12:00"),
DateTime.jd(-213447716, 23, 59, 59, "-12:00"),

@wtn

wtn commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Some dates no longer survive a Marshal round trip:

d = Date.new(-4713, 12, 31, Date::GREGORIAN)
d.marshal_dump[0, 2]          # => [-1, 213447754]
Marshal.load(Marshal.dump(d)) # => Date::Error: invalid jd

Date.new and Date.jd can store the same day with different jd values:

Date.new(579687, 12, 31, Date::GREGORIAN).marshal_dump[0, 2] # => [0, 213447754]
Date.jd(213447754, Date::GREGORIAN).marshal_dump[0, 2]       # => [1, 37]

Date.jd keeps jd below CM_PERIOD. Date.new computes jd from the year, and for a Gregorian date in the last 38 days of a period the result is above CM_PERIOD, up to CM_PERIOD + 37. A DateTime with a negative offset can be one day higher.

[0, 213447718, 0, 0, 0, 2299161.0] in the new test is the dump of Date.new(579687, 11, 25).

Changing the check to jd > CM_PERIOD + 38 lets these load and still rejects 213447756.

Date.new and DateTime.new can store a jd above CM_PERIOD for the last
days of a Gregorian period, so such dates failed to round-trip.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame

mame commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Thank you all. I haven't been able to follow every detail, but for now I changed the check to CM_PERIOD + 38. Is there anything else that should be fixed?

@wtn

wtn commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for the update. The crash is gone with this branch, and round trips work with the new jd range.

Two small points about the new tests:

  • The three assertions for df 86400, sf -1 and sf 1_000_000_000 use 0.0 as the start. 0.0 is an invalid start and raises Date::Error by itself, so these assertions pass without the df and sf range checks. Using 2299161.0 would test the range checks.
  • DateTime.new(2001, 2, 3, 4, 5, 6, "+24:00") gets offset 0 with a warning, because the string "+24:00" is not accepted as an offset. Rational(1) gives offset 86400.

One more thing from AI agent review: the 2-element and 3-element forms accept an out-of-range offset and a Float second fraction. It could be fixed here, or I can send a separate PR later.

Details

(Edited: the offset part was already fixed on master by #205, so only the Float case is left here.)

When the first element of the old forms is a Float, old_to_new stores the second fraction as a Float. The 6-element form now rejects a Float second fraction, so the resulting DateTime does not survive a Marshal round trip:

y = DateTime.allocate.marshal_load([2451545.25, 0, 2299161.0])
y.marshal_dump[3]             # => 0.0
Marshal.load(Marshal.dump(y)) # => TypeError: invalid second fraction (expected an Integer or Rational)

The round trip succeeds on master. The old forms do not seem to crash.

I think adding the same Integer or Rational check for sf in old_to_new would be enough:

    if (!RB_INTEGER_TYPE_P(*rsf) && !RB_TYPE_P(*rsf, T_RATIONAL))
	rb_raise(rb_eTypeError, "invalid second fraction (expected an Integer or Rational)");

With this change the example raises TypeError at marshal_load, and the tests pass. This line could go in the test:

assert_raise(TypeError) { DateTime.allocate.marshal_load([2451545.25, 0, 2299161.0]) }

The df and sf tests used the invalid start 0.0, so they passed without
the range checks. The "+24:00" offset was not accepted and became 0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame

mame commented Nov 25, 2026

Copy link
Copy Markdown
Member Author

Thank you! I think I fixed both points about the tests. As for the 2-element and 3-element forms, I'd appreciate it if you could send a separate PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants